package com.zcah.sensitive.service;

import com.zcah.sensitive.dao.SenInfoMetaRepository;
import com.zcah.sensitive.entity.ComprehensiveInformation;
import com.zcah.sensitive.entity.Links;
import com.zcah.sensitive.entity.Nodes;
import com.zcah.sensitive.entity.SenInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SenInfoServiceImpl implements SenInfoService {

    @Autowired
    private SenInfoMetaRepository senInfoMetaRepository;

   /* @Override
    public Map<String, Object> getDate(Long unixTimestamp) {
        Map<String, Object> dateInfo = new HashMap<>();
        SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat time_format = new SimpleDateFormat("HH");
        Pageable onePage = new PageRequest(1, 1);
        String date = date_format.format(unixTimestamp);
        String time = time_format.format(unixTimestamp);
        System.out.println(unixTimestamp);
        System.out.println(date);
        System.out.println(time);
        if (senInfoMetaRepository.findSenInfosByDateAndTime(date, time) != null) {
            Map<String, String> nowDate = new HashMap<>();
            nowDate.put("Date", date);
            nowDate.put("Time", time);
            dateInfo.put("now", nowDate);

            Long yesterday = unixTimestamp - 86400000;
            String yesterdayDate = date_format.format(yesterday);
            String yesterdayTime = time_format.format(yesterday);
            List<SenInfo> senInfos = senInfoMetaRepository.findSenInfosByDateAndTime(yesterdayDate, yesterdayTime);
            System.out.println(senInfos.size());
            if (senInfoMetaRepository.findSenInfosByDateAndTime(yesterdayDate, yesterdayTime).size() != 0) {
                dateInfo.put("yesterday", yesterdayDate);
            } else {
                dateInfo.put("yesterday", null);
            }

            Long tomorrow = unixTimestamp + 86400000;
            String tomorrowDate = date_format.format(tomorrow);
            String tomorrowTime = time_format.format(tomorrow);
            if (senInfoMetaRepository.findSenInfosByDateAndTime(tomorrowDate, tomorrowTime).size() != 0) {
                dateInfo.put("tomorrow", tomorrowDate);
            } else {
                dateInfo.put("tomorrow", null);
            }
        }
        return dateInfo;
    }*/

    @Override
    public ComprehensiveInformation getInfo(String unixTimestamp) {
        List<Nodes> nodes = new ArrayList<>();
        List<Links> links = new ArrayList<>();
        Map<String, Object> dateInfo = new HashMap<>();
        List<SenInfo> SenInfos = senInfoMetaRepository.findSenInfosByDate(unixTimestamp);
        // Get the distinct SenType number
        Set<String> senTypeSet = new HashSet<>();
        Set<String> ServerIPSet = new HashSet<>();
        for (SenInfo senInfo : SenInfos) {
            senTypeSet.add(senInfo.getSenType());
            ServerIPSet.add(senInfo.getServerIP());
        }
        // Get the Nodes
        for (String senType : senTypeSet) {
            nodes.add(new Nodes(senType));
        }
        for (String serverIP : ServerIPSet) {
            nodes.add(new Nodes(serverIP));
        }
        // Set Links
        for (String senType : senTypeSet) {
            links.add(new Links("敏感信息", senType, 0, ""));
            for (String serverIP : ServerIPSet) {
                links.add(new Links(senType, serverIP, 0, ""));
            }
        }

        for (String senType : senTypeSet) {
            for (SenInfo senInfo : SenInfos) {
                for (Links link : links) {
                    if (senType.equalsIgnoreCase(senInfo.getSenType()) && senType.equalsIgnoreCase(link
                            .getTarget())) {
                        link.setValue(link.getValue() + senInfo.getSenSize());
                        link.setInfo(link.getInfo().concat("\r\t").concat(senInfo.getHdfsPwd()));
                    }
                }
            }
        }
        for (String senType : senTypeSet) {
            for (String serverIP : ServerIPSet) {
                for (SenInfo senInfo : SenInfos) {
                    for (Links link : links) {
                        if (serverIP.equalsIgnoreCase(link.getTarget())
                                && serverIP.equalsIgnoreCase(senInfo.getServerIP())
                                && senInfo.getSenType().equalsIgnoreCase(link.getSource())
                                && senType.equalsIgnoreCase(link.getSource())) {
                            link.setValue(link.getValue() + senInfo.getSenSize());
                            link.setInfo(link.getInfo().concat("\r\n").concat(senInfo.getHdfsPwd()));
                        }
                    }

                }
            }
        }
        return new ComprehensiveInformation(nodes, links);
    }

}
